package com.leyou.sms.service.listener;

import com.aliyuncs.exceptions.ClientException;
import com.leyou.sms.service.config.SmsProperties;
import com.leyou.sms.service.utils.SmsUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;

@Component
@EnableConfigurationProperties(SmsProperties.class)
public class SmsListener {

    @Autowired
    private SmsProperties props;
    @Autowired
    private SmsUtils smsUtils;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "leyou.sms.queue",durable = "true"),
            exchange = @Exchange(value = "leyou.sms.exchange",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
            key = {"sms.verify.code"}
    ))
    public void smsListener(Map<String,String> map) throws ClientException {
        String phone = map.get("phone");
        String code = map.get("code");
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)){
            return;//不处理
        }
        smsUtils.sendSms(phone, code, props.getSignName(), props.getVerifyCodeTemplate());
        //throw new RuntimeException();//发送失败
    }


}
